<?php

class Comment extends CI_Controller {

    private static $layout_data = array();
    private static $home_data = array();
    
    function __construct()
    {
        parent::__construct();
        
        self::$home_data = array();
        $this->load->model('comment_model');
    }
	
    function index()
    {
        //Get page params
        $status = 0;
        $limit = 20;
        $offset = $this->uri->segment(4)?$this->uri->segment(4):0;
        
        //Get list comment unverify
        $arrComment = $this->comment_model->getListCommentByStatus($status, $limit, $offset);
        
        //Check total and load paging
        if($arrComment['total'] > $limit)
        {
            $this->load->library('pagination');

            $config['base_url'] = site_url() . '/comment/index/offset/';
            $config['total_rows'] = $arrComment['total'];
            $config['per_page'] = $limit;
            $config['uri_segment'] = 4;

            $this->pagination->initialize($config);
            self::$home_data['paging'] = TRUE;
        }
        
        //Assign toview
        self::$home_data['offset'] = $offset;
        self::$home_data['arrComment'] = $arrComment;
        
        self::$layout_data['content'] = $this->load->view('comment/index', self::$home_data, TRUE);
        $this->load->view('layouts/main', self::$layout_data);
    }
    
    /**
     * view comment by nodeID
     */
    public function view()
    {
        //Get page params
        $nodeID = $this->uri->segment(4)?$this->uri->segment(4):0;
        $limit = 20;
        $offset = $this->uri->segment(6)?$this->uri->segment(6):0;
        
        //Get list comment unverify
        $arrComment = $this->comment_model->getListCommentByNodeID($nodeID, $limit, $offset);
        
        //Check total and load paging
        if($arrComment['total'] > $limit)
        {
            $this->load->library('pagination');

            $config['base_url'] = site_url() . '/comment/view/id/'.$nodeID.'/offset/';
            $config['total_rows'] = $arrComment['total'];
            $config['per_page'] = $limit;
            $config['uri_segment'] = 6;

            $this->pagination->initialize($config);
            self::$home_data['paging'] = TRUE;
        }
        
        //Assign toview
        self::$home_data['nodeID'] = $nodeID;
        self::$home_data['offset'] = $offset;
        self::$home_data['arrComment'] = $arrComment;
        
        self::$layout_data['content'] = $this->load->view('comment/view', self::$home_data, TRUE);
        $this->load->view('layouts/main', self::$layout_data);
    }
    
    /**
     * Update comment status
     */
    public function updateCommentStatus()
    {
        //Get params
        $commentID = $this->input->post('cm_id');
        $status = $this->input->post('status');
        
        if($commentID <= 0)
        {
            //Return data
            echo 0; exit();
        }        
        
        //Update status
        $result = $this->comment_model->updateCommentStatus($commentID, $status);
        
        //Return data
        echo $result; exit();
    }
    
    /**
     * Delete comment
     */
    public function delete()
    {
        //Get CommentID
        $commentID = $this->uri->segment(4);

        //Delete node
        $this->comment_model->deleteComment($commentID);

        //Redirect
        redirect($_SERVER['HTTP_REFERER']);
    }
}
